const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA_TYPE = {
    max:{
        fixedhp: 299,
        fixedAtt: 19,
        fixdefence: 23,
        reaction : 23,
        attRate: 5.8, 
        hpRate: 5.8, 
        criticalRate: 3.9,
        criticalStrike: 7.8,
        defRate: 7.3,
        charging: 6.5
    },
    mid:{
        fixedhp: 254,   // 209  239   269   299
        fixdefence: 19.75, // 16   19  21  23
        fixedAtt: 16.75,   //  14   16   18  19
        reaction : 19.75,  // 16  19   21   23
        attRate: 4.957,  // 4.1   4.7   5.3  5.8
        hpRate: 4.957,   // 4.1  4.7  5.3  5.8
        criticalRate: 3.3,   //  2.7  3.1   3.5   3.9
        criticalStrike: 6.6,  //  5.4  6.2  7  7.8
        defRate: 6.2,   //  5.1  5.8  6.6   7.3
        charging: 5.5  //  4.5   5.2   5.8   6.5
    }
}

const CLIPDATA = CLIPDATA_TYPE.mid

const CONFIG = {
    chance: 20,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        criticalStrike: 62.2,
        dmgRate: 46.6,
        attRate: 46.6
    },
    clips: ["reaction", "attRate", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "reaction", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "reaction", "attRate", "criticalRate"]
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        { reaction: 80 + 50*3,attRate:0}, // 饰金之梦
        // { fixedAtt: 1202, attRate:20},  // 班尼
        // { dmgRate: 50, attRate:40, reaction: 200, tianfu1dmg: 32},  // 万叶
        // { criticalStrike: 60, fixedAtt: 790}, // 九条
    ]
}


let charaters = {
    units : {
        name: "八重",
        charging: 100,
        defence: 569,
        eDmgAdd:0,
        hp:11284,
        base: 340,    
        criticalRate: 24.2,    
        criticalStrike: 50,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}

// 普攻9： 67.42% 65.48% 96.71%  242.92%  260.76%    普攻10: 71.39% 69.33% 102.4% 257.21%  280.57%

// e 描述：迅速移动 留下一株 杀生樱   杀生樱间歇对周围一名敌人降下落雷,造成雷元素伤害
// 附近存在其他杀生樱时将提高位阶 不同位阶倍率不同  杀生樱最高同时存在3个 无命座阶级最多提升3级
// 杀生樱倍率:9级e  位阶1: 103.14%  位阶2: 128.93%   位阶3: 161.16%  位阶4: 201.45%
// 10级e           位阶1: 109.21%  位阶2: 136.51%   位阶3: 170.64%  位阶4: 213.30%
// 13级e           位阶1: 128.93%  位阶2: 161.16%   位阶3: 201.45%  位阶4: 251.81%

// q 描述： 降下雷电 造成雷元素范围伤害    解放杀生樱 每个杀生樱降下一道天狐雷霆 造成雷元素范围伤害
// 9级q:  442%雷伤   567.49%天狐雷霆   10级q:  468%雷伤   600.87%天狐雷霆   13级q:  552.5%雷伤   709.36%天狐雷霆  

// 天赋  合成天赋书时 25%几率随机得到一个本地区的天赋素材
// 3e q 3e
// 精通每点提高 杀生樱 0.15% 增伤 比如100精通 就是e多15%增伤

// 命座1  每个天狐雷霆恢复8能量
// 命座2  攻击范围+160%  e 初始位阶为2位  上限提高到4位阶  
// 命座3  e + 3
// 命座4  e 攻击命中敌人时候 全队雷伤+20 持续5秒
// 命座5  q + 3
// 命座6   e无视敌人60%防御

/** 神子后台 
 *  0命1精 268446
 *  2命1精 302001
 *  3命1精 330579
 *  4命1精 361924
 *  5命1精 381484
 *  6命1精 490193
 *  6命5精 564694
 */ 
let conns = 6;
calc(calc_clips_initial());
function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        if(weapon.weapontype == "magic") {
            charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
                count++;
                data = objValueAdd([data, ...CONFIG.buffs]);
                data.hp = data.hp*(1 + (data.hpRate||0)/100) + 4780;
                if(weapon.skill) { weapon.skill(data); }
                data.eDmgAdd += data.reaction * 0.15; // 天赋

                let jhDmgZhi = 1447 * 1.15 * (1 + (5*data.reaction)/(data.reaction+1200))
                let jf = 0
                if(conns >= 6) {
                    jf = 60
                }
                if(conns >= 4) {
                    data.dmgRate += 20  // 4命
                }
                let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:CONFIG.reactionDmgAdd }
                let a1Dmg = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.tianfu1dmg||0)}, rate: 0.7139}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let a2Dmg = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.tianfu1dmg||0)}, rate: 0.6933}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let a3Dmg = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.tianfu1dmg||0)}, rate: 1.024}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let aZDmg = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.tianfu1dmg||0)}, rate: 2.5721}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                // 阶1: 109.21%  位阶2: 136.51%   位阶3: 170.64%  位阶4: 213.30%
                // 128.93%  位阶2: 161.16%   位阶3: 201.45%  位阶4: 251.81%
                let eRate = [1.0921, 1.3651, 1.7064, 2.133]
                if(conns >= 3) {
                    eRate = [1.2893, 1.6116, 2.0145, 2.5181]
                }

                let e1Dmg1 = getDmg({ data:{...data, dmgRate:data.dmgRate + data.eDmgAdd + (data.eDmgAddLimit||0)*1}, rate: eRate[0]}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, jf); //
                let e1Dmg2 = getDmg({ data:{...data, dmgRate:data.dmgRate + data.eDmgAdd + (data.eDmgAddLimit||0)*2}, rate: eRate[1]}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, jf); //
                let e1Dmg3 = getDmg({ data:{...data, dmgRate:data.dmgRate + data.eDmgAdd + (data.eDmgAddLimit||0)*3}, rate: eRate[2]}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, jf); //
                let e1Dmg4 = getDmg({ data:{...data, dmgRate:data.dmgRate + data.eDmgAdd + (data.eDmgAddLimit||0)*3}, rate: eRate[3]}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, jf); //
                
                let e1Dmg1jh = getDmg({ data:{...data, 
                    dmgRate:data.dmgRate + data.eDmgAdd + (data.eDmgAddLimit||0)*1,
                    dmgZhi: (data.dmgZhi||0) + jhDmgZhi
                }, rate: eRate[0]}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, jf); //
                let e1Dmg2jh = getDmg({ data:{...data, 
                    dmgRate:data.dmgRate + data.eDmgAdd + (data.eDmgAddLimit||0)*2,
                    dmgZhi: (data.dmgZhi||0) + jhDmgZhi
                }, rate: eRate[1]}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, jf); //
                let e1Dmg3jh = getDmg({ data:{...data, 
                    dmgRate:data.dmgRate + data.eDmgAdd + (data.eDmgAddLimit||0)*3,
                    dmgZhi: (data.dmgZhi||0) + jhDmgZhi
                }, rate: eRate[2]}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, jf); //
                let e1Dmg4jh = getDmg({ data:{...data, 
                    dmgRate:data.dmgRate + data.eDmgAdd + (data.eDmgAddLimit||0)*3,
                    dmgZhi: (data.dmgZhi||0) + jhDmgZhi
                }, rate: eRate[3]}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, jf); //
    
                let qRate = [4.68, 6.0087]
                if(conns >= 5) {
                    qRate = [5.525, 7.0936]
                }
                // 10级q:  468%雷伤   600.87%天狐雷霆   13级q:  552.5%雷伤   709.36%天狐雷霆  
                let qDmg = getDmg({ data:{...data, dmgRate:data.dmgRate}, rate: qRate[0] + qRate[1]*3}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let qDmgbj = getDmg({ data:{...data, dmgRate:data.dmgRate}, rate: qRate[0] + qRate[1]*3}).bj * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
          
                if(data.phyRate) { // 物理特效伤害
                    data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
                }
                // 2153攻击   200爆伤  73.6增伤 51edmg
                let point = (a1Dmg + a2Dmg + a3Dmg) * 0 + aZDmg * 0 + qDmg + 
                e1Dmg1*0 + e1Dmg2 * 2 + e1Dmg2jh + e1Dmg3 * 6 + e1Dmg3jh * 3 + e1Dmg4 * 0;
                if(conns >= 2) {
                    point = (a1Dmg + a2Dmg + a3Dmg) * 0 + aZDmg * 0 + qDmg + 
                    e1Dmg3 * 2 + e1Dmg3jh * 1 + e1Dmg4 * 6 + e1Dmg4jh * 3;
                }
                let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
                return {point, data, second, attack, msg:{a1Dmg, a2Dmg,a3Dmg,aZDmg,e1Dmg1,e1Dmg2,e1Dmg3,e1Dmg4, qDmg,qDmgbj}}
            }))
        }
      
    })
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    console.log(charactersDmg[5]);
    console.log(charactersDmg.map((v, n)=>{
        return  `${n}得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                })
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second);
    });
    _ret.sort((b,a)=> a.point-b.point);
    return _ret[0];
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        });
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
